<template>
  <div class="alipay-config">
    <div class="config-list">
      <div class="channel-up">
        <div class="channel-up-title">
            <span>通道配置</span>
        </div>
        <div class="channel-up-content">
            <img src="../../../../../static/img/ofc_icon.png" alt="加载失败"/>
            <span id="span-channel-1">官方通道</span><span id="span-channel-2">(已配置)</span><pre/>
            <span id="span-channel-3" style="cursor: pointer;" @click="parameterConfiguration">参数配置</span>
            <!-- <span style="cursor: pointer;" id="span-channel-4" @click="rateConfiguration">费率配置</span> -->
        </div>
      </div>
    </div>
    <div class="official-model" v-if="model">
      <!--支付宝参数配置-->
      <div class="official-alipay-configuration" v-if="alipayParameter">
        <div class="off-con-title">
          <span>官方通道参数配置</span>
          <i class="el-icon-close" @click="close"></i>
        </div>
        <div class="off-con-content">
          <el-form label-width="130px">
            <el-form-item class="off-con-list" label="商户ID">
              <el-input style="width:65%" disabled v-model="config.shopId" clearable placeholder="商户ID"></el-input>
            </el-form-item>
            <el-form-item class="off-con-list" label="支付宝应用APPID">
              <el-input style="width:65%" disabled v-model="config.appId" clearable placeholder="支付宝应用APPID"></el-input>
            </el-form-item>
            <el-form-item class="off-con-list" label="支付宝公钥">
               <el-input style="width:65%" disabled type="password" v-model="config.alipayPubKey" clearable placeholder="支付宝应用公钥"></el-input>
            </el-form-item>
            <div class="auth-box">
              <img class="auth-img" :src="imgUrl" :alt="imgAlt">
              <div class="auth-time"><el-button type="text" @click="getCode" v-html="code.message"></el-button></div>
            </div>
          </el-form>
        </div>
        <div class="off-con-button">
          <el-button type="primary" @click="handleSubmit">校验授权</el-button>
          <el-button @click="close">关闭</el-button>
        </div>
      </div>
      <!--支付宝费率配置-->
      <div class="official-alipay-rate" v-if="alipayRate">
        <div class="off-con-title">
          <span>官方通道费率</span>
          <i class="el-icon-close" @click="close"></i>
        </div>
        <div class="off-con-content">
          费率配置
        </div>
        <div class="off-con-button">
          <el-button type="primary">提交</el-button>
          <el-button type="warning" @click="close">关闭</el-button>
        </div>
      </div>
      <!--支付宝配置-->
      <div class="alipay-model" v-else-if="alipayConfiguration">
        <div class="off-con-title">
          <span>支付宝刷卡支付配置</span>
          <i class="el-icon-close" @click="close"></i>
        </div>
        <div class="off-con-content">
           <span>默认官方通道</span>
        </div>
        <div class="off-con-button">
          <el-button type="primary">提交</el-button>
          <el-button type="warning" @click="close">关闭</el-button>
        </div>
      </div>
    </div>
        <div class="container-down">
          <div class="weChat-pay-down">
            <div class="weChat-pay-title">支付宝支付配置</div>
            <div class="">
              <table border="0" cellspacing="0">
                <thead>
                  <tr>
                    <th><div class="table-cell">名称</div></th>
                    <th><div class="table-cell">支付通道</div></th>
                    <th><div class="table-cell">支付费率</div></th>
                    <th><div class="table-cell">结算周期</div></th>
                    <th><div class="table-cell">操作</div></th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="(cell,index) in typeList" :key="index" >
                    <td><div class="table-cell">{{this.$route.params.type}}</div></td>
                    <td><div class="table-cell">{{this.$route.params.type}}</div></td>
                    <td><div class="table-cell">{{this.$route.params.type}}</div></td>
                    <td><div class="table-cell">{{this.$route.params.type}}</div></td>
                    <td><div class="table-cell"><span id="span-channel-3" style="cursor: pointer;" @click="weChatPayModel()">配置</span></div></td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>

  </div>
</template>

<script>
export default {
  name:'alipay-config',
  data () {
    return {
      channelRate:"",
      appChannelRate:"",
      imgUrl:"",
      imgAlt:"授权码获取失败！请重新获取！",
      code:{
          message:"获取授权码",
          get_again:true,
          wait_time:60
      },
       config:{
         appId:"",
         shopId:"",
         alipayPubKey:""
       },
       typeList:[],
        model:false,
        alipayParameter: false,
        alipayRate:false,
        alipayConfiguration:false,
    };
  },
  props:{
      shopId:{
          type:String,
          default:function(){
              return 0
          }
      }
  },
  created() {
       this.config.shopId = this.shopId;
  },
  mounted() {},
  methods: {
    initConfig(){
       let token = sessionStorage.getItem(1);
       let url = `paymentchannel/alipay/query/${token}`;
       let params = `shopId=${this.shopId}`; 
       this.$Http.post(url,params).then(res=>{
         if(res.data.resp_code === "000000"){
            if(res.data.result.status == 1){
              this.config = res.data.result;
              this.imgAlt = "已授权成功";
              this.code.get_again = false;
              this.code.message = "已授权成功";
            }else{
              this.getCode();
            }
         }else{
            this.$notify.info({
              title: '消息',
              message: res.data.resp_message
            }); 
         }
       }).catch(err=>{
            this.$notify.error({
              title: '提示',
              message: "服务器繁忙！请稍后重试！"
            }); 
       })
    },
    getCode(){
          // 获取验证码
          if(this.code.get_again){
              // 请求.. 
            let token = sessionStorage.getItem(1);
            let url = `paymentchannel/alipay/query/${token}`;
            let params = `shopId=${this.shopId}&isCode=${true}`; 
            this.$Http.post(url,params).then(res=>{
                if(res.data.resp_code === "000000"){
                  this.imgUrl = res.data.result.codePath;
                }else{
                  this.$notify.info({
                    title: '消息',
                    message: res.data.resp_message
                  }); 
                }
            }).catch(err=>{
              this.$notify.error({
                title: '提示',
                message: "服务器繁忙！请稍后重试！"
              }); 
            })

            this.code.get_again = false;
            let timer = setInterval(()=>{
                if(this.code.wait_time === 0){
                    this.code.get_again = true;
                    this.code.wait_time = 60;
                    this.code.message = "重新获取";
                    this.imgUrl = "";
                    clearInterval(timer);
                }else{
                    this.code.wait_time = this.code.wait_time - 1;
                    this.code.message = `${this.code.wait_time}秒后重新获取`
                }
            },1000)
             
          } 


      },
    //参数配置
    parameterConfiguration(){
      this.model=true;
      this.alipayParameter=true;
      this.initConfig();
    },
    //费率配置
    rateConfiguration(){
      this.model=true;
      this.alipayRate=true;
    },
    //配置
    weChatPayModel(){
      this.model=true;
      this.alipayConfiguration=true;
    },
    //关闭
    close(){
      this.model=false;//透明背景
      this.alipayParameter=false;
      this.alipayRate=false;
      this.alipayConfiguration=false;
    },
    handleSubmit(){
       // 校验配置
      let token = sessionStorage.getItem(1);
      let url = `paymentchannel/alipay/supply/${token}`;
      let params = 'shopId=' + this.shopId;
      this.$Http.post(url,params).then(res=>{
        if(res.data.resp_code === "000000"){
          this.model=false;
          this.alipayParameter=false;
          this.$message.success("授权完成！");
        }else{
          this.$message.error("授权操作失败！请联系管理员！");
        }
      }).catch(err=>{
          this.$message.error("服务器繁忙！无法验证！");
      })
    }

  }
}

</script>
<style scoped>
.channel-up{
  width: 100%;
  height: 125px;
}
.channel-up-title{
  height: 50px;
  line-height: 50px;
  font-size: 20px;
  color: rgb(139, 136, 136);
  padding: 7px 0 0 20px;
}
.channel-up-content>img{
  width: 50px;
  height: 50px;
  float: left;
  padding: 3px 5px 5px 20px;
}
.container-down{
  border: 1px solid #ededed;
  margin-top: 10px;
}
.weChat-pay-title{
  height: 50px;
  line-height: 50px;
  font-size: 20px;
  color: rgb(139, 136, 136);
  padding: 7px 0 0 20px;
}
.official-model{
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background-color:rgba(71, 68, 68, 0.438)
}
.official-alipay-configuration{
  width: 550px;
  position: absolute;
  top: 10%;
  left: 35%;
  border-radius: 1%;
  background-color: #F5F7FA
}
.off-con-content{
  padding: 10px 35px 0 35px;
}
.off-rate-list{
  height: 70px;
  padding: 20px 0 0 12%;
  border-bottom: #dfe4f0 1px solid;
}
.off-c-img{
  width: 100px;
  height: 100px;
}
.off-c-img>img{
  width: 100px;
  height: 100px;
}
.off-con-title{
  height: 55px;
  line-height: 55px;
  font-size: 18px;
  font-family: "PingFang SC";
  color: rgb(70, 69, 69);
  background-color: rgb(240, 234, 234);
  border-top-right-radius: 8%;
  border-top-left-radius: 8%;
  padding-left: 20px;
  margin-bottom: 10px;
  border-bottom: 1px solid rgb(221, 214, 214)
}
.off-con-list{
  padding-bottom:20px;
  border-bottom: #dfe4f0 1px solid;
}
.off-con-title>i{
  position: absolute;
  top: 15px;
  right: 10px;
}
.off-con-title>i:hover{
  position: absolute;
  top: 15px;
  right: 10px;
  color: red;
  cursor: pointer;
}
.off-con-button{
  line-height: 50px;
  float: right;
  padding: 0 45px 15px 0;
}
.official-alipay-rate{
  width: 550px;
  position: absolute;
  top: 10%;
  left: 35%;
  border-radius: 1%;
  background-color: #F5F7FA
}
.alipay-model{
  width: 550px;
  position: absolute;
  top: 10%;
  left: 35%;
  border-radius: 1%;
  background-color: #F5F7FA
}

table{
  background-color: #FFF;
  width: 100%;
  font-size: 12px;
  min-width: 900px;
}
th{
  position: relative;
  word-wrap: normal;
  text-overflow: ellipsis;
  vertical-align: middle;
  border-bottom: 1px solid #ebeef5;
    
}
thead{
  padding: 8px 0;
  color: #909399;
}
tr{
  padding: 8px 0;
}
tr:hover{
  background-color: #F5F7FA;
}
td{
  border: none;
  border-bottom: 1px solid #ebeef5;
}
.table-cell{
  white-space: normal;
  word-break: break-all;
  line-height: 22px;
  padding: 10px 10px;
  text-align: center;
  min-width: 40px;
  box-sizing: border-box;
}

#setTimout-span{
  color: red;
  padding-left: 20px;
}
#span-channel-1{
  font-size: 16px;
}
#span-channel-2{
  font-size: 16px;
  color: rgb(40, 190, 103);
}
#span-channel-3,#span-channel-4{
  font-size: 14px;
  color: rgb(35, 158, 240);
  padding-right: 10px;
}
#span-rate{
  font-size: 12px;
  color: #626466;
  padding-left: 10px;
}
.auth-box{
  height: 300px;
  text-align: center;
}
.auth-img{
  height: 300px;
  width: 300px;
}
</style>